package com.broadengate.view.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.broadengate.bean.BillInfo;
import com.broadengate.bean.ContractInfo;
import com.broadengate.init.IsmpInit;
import com.broadengate.service.BillService;
import com.broadengate.service.ContractService;
import com.broadengate.service.ServiceResult;
import com.broadengate.util.ExcelUtil;
import com.broadengate.util.JSONResult;
import com.broadengate.util.StringUtil;
import com.broadengate.view.model.BillModel;
import com.broadengate.view.model.ContractModel;
import com.broadengate.view.pageModel.DataGrid;

/**
 * 合同管理
 * 
 * @author 木子传
 *
 */
@Controller
@RequestMapping("/ContractController")
public class ContractController
{
    
    private final static Logger log = Logger.getLogger(LoginController.class);
    
    private String id;
    
    private String billcode;
    
    private String money;
    
    private String paymentsDate;
    
    private String billDate;
    
    private String billstatus;
    
    @Autowired
    private ContractService<ContractInfo> contractService;
    
    @Autowired
    private BillService<BillInfo> billService;
    
    @RequestMapping("/dataGrid")
    @ResponseBody
    public DataGrid queryContractList(ContractModel model, HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        
        log.info("inner contract.queryContractList");
        List<ContractInfo> list = contractService.queryByList(model);
        DataGrid dataGrid = new DataGrid();
        if (list != null & list.size() > 0)
        {
            dataGrid.setTotal(contractService.queryByCount(model));
        }
        
        dataGrid.setRows(list);
        
        return dataGrid;
    }
    
    @RequestMapping("/queryContractInfo")
    public String getContractInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        ContractInfo info = new ContractInfo();
        int contractId = StringUtil.getStrToInt(request.getParameter("contractId"));
        info = contractService.queryContractById(contractId);
        request.setAttribute("contractInfo", info);
        return "contract/contractedit";
    }
    
    @RequestMapping("/getBillList")
    @ResponseBody
    public DataGrid getBillList(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        String po = request.getParameter("po");
        BillModel model = new BillModel();
        model.setPo(po);
        List<BillInfo> billInfoList = billService.queryBillListByPo(model);
        DataGrid dataGrid = new DataGrid();
        if (billInfoList != null && billInfoList.size() > 0)
        {
            dataGrid.setTotal(billInfoList.size());
            dataGrid.setRows(billInfoList);
        }
        return dataGrid;
    }
    
    @RequestMapping(value = "search")
    public ModelAndView search(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        String contractName = request.getParameter("contractName");
        String contractStatus = request.getParameter("contractStatus");
        ContractModel cmodel = new ContractModel();
        cmodel.setContractName(contractName);
        if (StringUtil.getStrToInt(contractStatus) != 0)
        {
            cmodel.setContractStatus(StringUtil.getStrToInt(contractStatus));
        }
        List<ContractInfo> list = contractService.search(cmodel);
        
        log.info("contractName=" + contractName + ";contractStatus=" + contractStatus);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("contractList", list);
        map.put("BaseModel", cmodel);
        map.put("contractName", contractName);
        map.put("contractStatus", contractStatus);
        
        return new ModelAndView("contractlist", map);
    }
    
    @RequestMapping("/updateBillInfo")
    @ResponseBody
    public JSONResult updateBillInfo(BillModel model) throws Exception
    {
        JSONResult json = new JSONResult();
        json.setMsg("修改成功");
        try
        {
            billService.billInfoUpdate(model);
            json.setSuccess(true);
        }
        catch (Exception e)
        {
            json.setMsg("修改失败");
            json.setSuccess(false);
        }
        return json;
    }
    
    @RequestMapping("/updateContractInfo")
    @ResponseBody
    public JSONResult updateContractInfo(ContractModel model) throws Exception
    {
        JSONResult json = new JSONResult();
        
        json.setMsg("修改成功");
        try
        {
            contractService.updateContractInfo(model);
            json.setSuccess(true);
        }
        catch (Exception e)
        {
            json.setMsg("修改失败");
            json.setSuccess(false);
        }
        return json;
    }
    
    /**
    * 新增部门信息
    * <功能详细描述>
    * @param model
    * @return [参数说明]
    * 
    * @return JSONResult [返回类型说明]
    * @exception throws [违例类型] [违例说明]
    * @see [类、类#方法、类#成员]
    */
    @RequestMapping("/addContract")
    @ResponseBody
    public JSONResult addContract(ContractModel model)
    {
        JSONResult result = new JSONResult();
        
        result.setMsg("新增成功");
        try
        {
            contractService.addContractInfo(model);
            result.setSuccess(true);
        }
        catch (Exception e)
        {
            result.setMsg("新增失败");
            result.setSuccess(false);
        }
        
        return result;
    }
    
    /**
     * 删除订单信息
     * <功能详细描述>
     * @param id
     * @return [参数说明]
     * 
     * @return JSONResult [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("/delContracts")
    @ResponseBody
    public JSONResult delContracts(String[] contractids)
    {
        JSONResult result = new JSONResult();
        try
        {
            result.setMsg("删除成功");
            if (contractids != null && !"".equals(contractids) && contractids.length > 0)
            {
                for (String contractid : contractids)
                {
                    contractService.delContracts(contractid);
                }
            }
            result.setSuccess(true);
        }
        catch (Exception e)
        {
            log.error(e);
            result.setMsg("修改失败");
            result.setSuccess(false);
        }
        return result;
    }
    
    /**
     * 删除订单信息
     * <功能详细描述>
     * @param id
     * @return [参数说明]
     * 
     * @return JSONResult [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("/delbillInfos")
    @ResponseBody
    public JSONResult delbillInfos(String[] ids)
    {
        JSONResult result = new JSONResult();
        try
        {
            result.setMsg("删除成功");
            if (ids != null && !"".equals(ids) && ids.length > 0)
            {
                for (String id : ids)
                {
                    contractService.delbillInfos(id);
                }
            }
            result.setSuccess(true);
        }
        catch (Exception e)
        {
            result.setMsg("删除失败");
            result.setSuccess(false);
        }
        return result;
    }
    
    /**
     * 新增部门信息
     * <功能详细描述>
     * @param model
     * @return [参数说明]
     * 
     * @return JSONResult [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("/addBillInfo")
    @ResponseBody
    public JSONResult addBillInfo(BillModel model)
    {
        JSONResult result = new JSONResult();
        
        result.setMsg("新增成功");
        try
        {
            contractService.addBillInfo(model);
            result.setSuccess(true);
        }
        catch (Exception e)
        {
            result.setMsg("修改失败");
            result.setSuccess(false);
        }
        
        return result;
    }
    
    /**
     * 批量导入
     * <功能详细描述>
     * @param model
     * @return [参数说明]
     * 
     * @return JSONResult [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("/batchContract")
    @ResponseBody
    public JSONResult batchContract(HttpServletRequest request, HttpServletResponse response)
    {
        JSONResult result = new JSONResult();
        // 转型为MultipartHttpRequest：     
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件：     
        MultipartFile file = multipartRequest.getFile("contractExcleFile");
        
        Collection collection = new ArrayList();
        ServiceResult sr = new ServiceResult();
        if (!file.isEmpty())
        {
            try
            {
                HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
                ExcelUtil eu = new ExcelUtil(IsmpInit.getProperties(), workbook, ContractModel.class);
                collection = eu.toCollection();
                sr = contractService.betchAddContractInfo((List<ContractModel>) collection);
                eu.showException();
            }
            catch (Exception e1)
            {
                // TODO Auto-generated catch block
                log.error(e1.getMessage(), e1);
                result.setMsg(e1.getMessage());
                result.setSuccess(false);
                return result;
            }
        }
        result.setSuccess(true);
        result.setObj(sr);
        return result;
    }
    
    public String getId()
    {
        return id;
    }
    
    public void setId(String id)
    {
        this.id = id;
    }
    
    public String getBillcode()
    {
        return billcode;
    }
    
    public void setBillcode(String billcode)
    {
        this.billcode = billcode;
    }
    
    public String getMoney()
    {
        return money;
    }
    
    public void setMoney(String money)
    {
        this.money = money;
    }
    
    public String getPaymentsDate()
    {
        return paymentsDate;
    }
    
    public void setPaymentsDate(String paymentsDate)
    {
        this.paymentsDate = paymentsDate;
    }
    
    public String getBillDate()
    {
        return billDate;
    }
    
    public void setBillDate(String billDate)
    {
        this.billDate = billDate;
    }
    
    public String getBillstatus()
    {
        return billstatus;
    }
    
    public void setBillstatus(String billstatus)
    {
        this.billstatus = billstatus;
    }
}
